
package nccloud.web.arap.amortization.action.tool;

import java.util.Map;

import nc.vo.pub.lang.UFDateTime;
import nc.vo.pubapp.pattern.model.entity.bill.IBill;
import nccloud.framework.core.exception.ExceptionUtils;
import nccloud.framework.core.reflect.Constructor;
import nccloud.framework.service.ServiceLocator;
import nccloud.pubitf.scmpub.pub.service.ISCMPubQueryService;
import nccloud.web.arap.amortization.action.base.OperateInfo;

/**
* @description 合并Ts工具
* @version ncc1.0
*/
public class CombinTsTool<T extends IBill> {

    private Class<T> clazz;

    public CombinTsTool(Class<T> clazz) {
        this.clazz = clazz;
        }

        /**
        *
        * 执行查询后台原始VO,进行合并
        *
        * @param infos
        * @return
        *
        */
    public T[] combinToOriginal(OperateInfo[] infos) {
        if (infos == null || infos.length == 0) {
            return null;
        }
        Map<String, UFDateTime> map = OperateInfo.convertToMap(infos);
        T[] bills = this.queryOriginal(map.keySet().toArray(new String[0]));
        if (bills == null || bills.length == 0) {
            ExceptionUtils.wrapBusinessException(nc.vo.ml.NCLangRes4VoTransl.getNCLangRes().getStrByID("4009012_0",
                "04009012-0003")/* @res "当前单据已被删除，请刷新重试！" */);
        }
        for (IBill bill : bills) {
            String id = bill.getParent().getPrimaryKey();
            UFDateTime currentTs = map.get(id);
            bill.getParent().setAttributeValue("ts", currentTs);
        }
        return bills;
    }

    /**
    *
    * 根据操作信息合并Ts
    *
    * @param infos
    * @return
    *
    */
    public T[] combin(OperateInfo[] infos) {
        if (infos == null || infos.length == 0) {
            return null;
        }
        Map<String, UFDateTime> map = OperateInfo.convertToMap(infos);
        String[] ids = map.keySet().toArray(new String[0]);
        T[] bills = Constructor.construct(clazz, ids.length);
        for (int i = 0; i < bills.length; i++) {
            bills[i] = Constructor.construct(clazz);
            String id = bills[i].getParent().getPrimaryKey();
            UFDateTime currentTs = map.get(id);
            bills[i].getParent().setAttributeValue("ts", currentTs);
        }
        return bills;
    }

    /**
    *
    * 查询原始单据
    *
    * @param ids
    * @return
    *
    */
    private T[] queryOriginal(String[] ids) {
        try {
            T[] bills = ServiceLocator.find(ISCMPubQueryService.class).billquery(clazz, ids);
            return bills;
        } catch (Exception e) {
            ExceptionUtils.wrapException(e);
        }
        return null;
    }

}
